# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import csv
import json

# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymongo
from urllib3.filepost import writer


# class TutorialPipeline:
    # def open_spider(self, spider):
    #     self.client = pymongo.MongoClient("mongodb://localhost:27017/")
    #     self.db = self.client.get_database("quotes")
    #     self.collection = self.db.get_collection("quote")
    #
    #     self.items = []
    #
    # def process_item(self, item, spider):
    #
    #     self.items.append(item)
    #     if len(self.items) == 15:
    #         self.collection.insert_many(self.items)
    #         self.items.clear()
    #
    #     return item
    #
    #
    # def close_spider(self, spider):
    #     if len(self.items)>0:
    #         self.collection.insert_many(self.items)
    #         self.items.clear()
    #
    #     self.client.close()

# class CSVPipline:
#     def open_spider(self, spider):
#         if spider.name == "4399":
#             self.file = open(f"{spider.name}.csv", "w", newline='', encoding="utf-8")
#             self.csv_writer = csv.writer(self.file)
#             self.csv_writer.writerow(["name", "img"])
#     def process_item(self, item, spider):
#         if spider.name == "4399":
#             self.csv_writer.writerow([item["name"], item["img"]])
#         return item
#
#     def close_spider(self, spider):
#         if spider.name == "4399":
#             self.file.close()




import redis

class ProducerPipline:
    def open_spider(self, spider):
        if spider.name == "producer":
            self.client = redis.Redis()
            self.client.delete('tasks')

    def process_item(self, item, spider):
        if spider.name == "producer":
            self.client.lpush("tasks", json.dumps({
                "url": "https://httpbin.org/post",
                "meta": {

                },
                "data": item,
                "method": "POST",
            }, ensure_ascii=False))
        return item

    def close_spider(self, spider):
        if spider.name == "producer":
            self.client.close()

